package com.njupt.DynamicProgramming;

/**
 * @Author: wujiaming
 * @CreateTime: 2025/2/23 18:46
 * @Description: 122. 买卖股票的最佳时机 II
 * 任何时候只能持有一份股票，但是在这个期间可以多次购买
 * @Version: 1.0
 */


public class MaxProfit2_122 {

    public int maxProfit(int[] prices) {
        int len = prices.length;
        int dp[][] = new int[len][2];
        dp[0][0] = 0 - prices[0];
        dp[0][1] = 0;
        for (int i = 1; i < len; i++) {
            dp[i][0] = Math.max(dp[i-1][0],dp[i-1][1] - prices[i]);
            dp[i][1] = Math.max(dp[i-1][1],dp[i-1][0] + prices[i]);
        }
        return Math.max(dp[len-1][0],dp[len-1][1]);
    }

    public static void main(String[] args) {
        int[] prices = {7,6,4,3,1};
        MaxProfit2_122 test = new MaxProfit2_122();
        System.out.println(test.maxProfit(prices));
    }
}
